<template>
  <div class="login-container">
    <!-- 背景装饰元素 -->
    <div class="background-decoration"></div>
    
    <!-- 登录卡片 -->
    <div class="login-card" :class="{ 'is-loading': isLoading }">
      <div class="login-header">
        <h1>账户登录</h1>
        <p>请输入您的账号信息</p>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <!-- 用户名输入 -->
        <div class="form-group">
          <label for="username">用户名</label>
          <div class="input-wrapper">
            <i class="icon-user"></i>
            <input 
              type="text" 
              id="username" 
              v-model="form.username" 
              @blur="validateField('username')"
              :class="{ 'invalid': formErrors.username }"
              placeholder="请输入用户名"
            >
          </div>
          <p class="error-message">{{ formErrors.username }}</p>
        </div>
        
        <!-- 密码输入 -->
        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-wrapper">
            <i class="icon-lock"></i>
            <input 
              :type="showPassword ? 'text' : 'password'" 
              id="password" 
              v-model="form.password" 
              @blur="validateField('password')"
              :class="{ 'invalid': formErrors.password }"
              placeholder="请输入密码"
            >
            <button 
              type="button" 
              class="toggle-password" 
              @click="showPassword = !showPassword"
            >
              <i :class="showPassword ? 'icon-eye-off' : 'icon-eye'"></i>
            </button>
          </div>
          <p class="error-message">{{ formErrors.password }}</p>
        </div>
        
        <!-- 记住密码和忘记密码 -->
        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="rememberMe">
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
        
        <!-- 登录按钮 -->
        <button type="submit" class="login-button" :disabled="isLoading">
          <span v-if="!isLoading">登录</span>
          <span v-if="isLoading" class="loading-spinner">
            <i class="icon-spinner"></i> 登录中...
          </span>
        </button>
        
        <!-- 注册链接 -->
        <div class="register-prompt">
          还没有账号? <a href="#">立即注册</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据
      form: {
        username: '',
        password: ''
      },
      // 错误信息
      formErrors: {
        username: '',
        password: ''
      },
      // 控制密码显示
      showPassword: false,
      // 记住密码
      rememberMe: false,
      // 加载状态
      isLoading: false
    };
  },
  methods: {
    // 验证单个字段
    validateField(field) {
      const value = this.form[field];
      this.formErrors[field] = '';
      
      if (!value) {
        this.formErrors[field] = field === 'username' ? '用户名不能为空' : '密码不能为空';
        return false;
      }
      
      if (field === 'username' && value.length < 3) {
        this.formErrors[field] = '用户名至少3个字符';
        return false;
      }
      
      if (field === 'password' && value.length < 6) {
        this.formErrors[field] = '密码至少6个字符';
        return false;
      }
      
      return true;
    },
    
    // 验证整个表单
    validateForm() {
      let isValid = true;
      
      Object.keys(this.form).forEach(field => {
        if (!this.validateField(field)) {
          isValid = false;
        }
      });
      
      return isValid;
    },
    
    // 处理登录
    handleLogin() {
      if (!this.validateForm()) {
        // 滚动到第一个错误字段
        const firstErrorField = document.querySelector('.invalid');
        if (firstErrorField) {
          firstErrorField.scrollIntoView({ behavior: 'smooth' });
        }
        return;
      }
      
      // 模拟登录请求
      this.isLoading = true;
      setTimeout(() => {
        this.isLoading = false;
        
        // 实际应用中这里会有API调用
        console.log('登录信息:', {
          username: this.form.username,
          password: this.form.password,
          rememberMe: this.rememberMe
        });
        
        // 登录成功后的操作，如跳转路由
        // this.$router.push('/dashboard');
        
        // 显示成功消息
        alert('登录成功！');
      }, 1500);
    }
  },
  mounted() {
    // 如果需要，可以在这里从localStorage加载记住的用户名
    if (localStorage.getItem('rememberedUsername')) {
      this.form.username = localStorage.getItem('rememberedUsername');
      this.rememberMe = true;
    }
  },
  watch: {
    // 监听记住密码状态变化
    rememberMe(newVal) {
      if (!newVal) {
        localStorage.removeItem('rememberedUsername');
      } else if (this.form.username) {
        localStorage.setItem('rememberedUsername', this.form.username);
      }
    },
    // 监听用户名变化，更新本地存储
    'form.username'(newVal) {
      if (this.rememberMe) {
        localStorage.setItem('rememberedUsername', newVal);
      }
    }
  }
};
</script>

<style scoped>
/* 基础样式 */
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
  position: relative;
  background-color: #f5f7fa;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 30% 20%, rgba(66, 153, 225, 0.1) 0%, transparent 40%),
              radial-gradient(circle at 70% 60%, rgba(102, 16, 242, 0.1) 0%, transparent 40%);
  z-index: 1;
}

/* 登录卡片 */
.login-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 30px;
  box-sizing: border-box;
  position: relative;
  z-index: 2;
  transition: all 0.3s ease;
}

.login-card.is-loading {
  opacity: 0.8;
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  margin: 0 0 10px;
  color: #1d2129;
  font-size: 24px;
  font-weight: 600;
}

.login-header p {
  margin: 0;
  color: #86909c;
  font-size: 14px;
}

/* 表单样式 */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  color: #4e5969;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
}

.form-group input {
  width: 100%;
  height: 48px;
  padding: 0 16px 0 42px;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.2s ease;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #4299e1;
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
}

.form-group input.invalid {
  border-color: #f56c6c;
}

.form-group input.invalid:focus {
  box-shadow: 0 0 0 3px rgba(245, 108, 108, 0.1);
}

/* 图标样式 */
.icon-user::before {
  content: "👤";
}

.icon-lock::before {
  content: "🔒";
}

.icon-eye::before {
  content: "👁️";
}

.icon-eye-off::before {
  content: "🙈";
}

.icon-spinner {
  display: inline-block;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.input-wrapper i {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: #86909c;
}

/* 切换密码显示按钮 */
.toggle-password {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #86909c;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.toggle-password:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

/* 错误信息 */
.error-message {
  margin: 0;
  font-size: 12px;
  color: #f56c6c;
  height: 16px;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 5px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #4e5969;
  cursor: pointer;
}

.remember-me input {
  width: 16px;
  height: 16px;
  accent-color: #4299e1;
}

.forgot-password {
  font-size: 14px;
  color: #4299e1;
  text-decoration: none;
  transition: color 0.2s;
}

.forgot-password:hover {
  color: #2b6cb0;
  text-decoration: underline;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 48px;
  background-color: #4299e1;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.login-button:hover {
  background-color: #3182ce;
}

.login-button:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

/* 注册提示 */
.register-prompt {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #86909c;
}

.register-prompt a {
  color: #4299e1;
  text-decoration: none;
}

.register-prompt a:hover {
  text-decoration: underline;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-card {
    padding: 24px 20px;
  }
  
  .login-header h1 {
    font-size: 22px;
  }
  
  .form-group input {
    height: 44px;
  }
  
  .login-button {
    height: 44px;
    font-size: 15px;
  }
}
</style>